package at.jku.se.fastsell;


import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import at.jku.se.fastsell.gui.AllAuctionsView;
import at.jku.se.fastsell.gui.AuctionDetailView;
import at.jku.se.fastsell.gui.EntryView;
import at.jku.se.fastsell.gui.IViewStatus;
import at.jku.se.fastsell.gui.NewAuctionView;

import com.vaadin.annotations.Theme;
import com.vaadin.navigator.Navigator;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.UI;

/**
 * Main class for FastSell web application
 */
@Theme("fastsell")
public class FastsellUI extends UI {
	
	// TODO enable polling (set to 5 or 10 seconds)
	public static final int POLL_INTERVAL = 3600 * 1000;
	
	private static final long serialVersionUID = 511089935415683713L;
	
	/** Navigator to change views */
	private static Navigator navigator;
	
	private static List<IViewStatus> views = new LinkedList<IViewStatus>();
	
	// ----------------------------------------------------------------------------------
	
	//@WebServlet(value = "/*", asyncSupported = true)
	//@VaadinServletConfiguration(productionMode = false, ui = FastsellUI.class, widgetset = "at.jku.se.fastsell.widgetset.FastsellWidgetset")
	public static class Servlet extends VaadinServlet {
		private static final long serialVersionUID = 1L;
	}
	
	// ----------------------------------------------------------------------------------
	
	@Override
	protected void init(VaadinRequest request) {
		this.getPage().setTitle("FastSell - The Auction Platform");
		this.setLocale(new Locale("EN"));
		this.setPollInterval(POLL_INTERVAL);

		navigator = new Navigator(this, this);
		
		EntryView entryView = new EntryView();
		AllAuctionsView allAuctionsView = new AllAuctionsView();
		AuctionDetailView auctionDetailView = new AuctionDetailView();
		NewAuctionView newAuctionView = new NewAuctionView();
		
		views.add(entryView);
		views.add(allAuctionsView);
		views.add(auctionDetailView);
		views.add(newAuctionView);
		
		navigator.addView(EntryView.NAME, entryView);
		navigator.addView(AllAuctionsView.NAME, new AllAuctionsView());
		navigator.addView(AuctionDetailView.NAME, new AuctionDetailView());
		navigator.addView(NewAuctionView.NAME, new NewAuctionView());
		
		navigator.navigateTo(EntryView.NAME);
	}
	
	// ----------------------------------------------------------------------------------
	
	public static void navigateTo(String viewname) {
		navigator.navigateTo(viewname);
		
		for (IViewStatus view : views) {
			if (!view.getName().equals(viewname))
				view.setInactive();
		}
	}

}